home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / jmod311.zip / JMODEM_C.C < prev    next >
Text File  |  1992-02-02  |  8KB  |  133 lines

  1. /****************************************************************************/
  2. /*   FILE JMODEM_C.C                                                        */
  3. /*   Created 11-JAN-1990                   Richard B. Johnson               */
  4. /*                                         405 Broughton Drive              */
  5. /*                                         Beverly, Massachusetts 01915     */
  6. /*                                         BBS (508) 922-3166               */
  7. /*                                                                          */
  8. /*   File I/O                                                               */
  9. /*   file_io();                                                             */
  10. /*   All of the file I/O is called from this one block to ease portability  */
  11. /*                                                                          */
  12. /****************************************************************************/
  13. #define FILE_IO                          /* For variable length param list  */
  14. #include <stdio.h>                       /* Used for FILE                   */
  15. #include <dos.h>                         /* Used for file-size              */
  16. #include <io.h>                          /* Ysed for Unix / DOS type files  */
  17. #include <fcntl.h>                       /* Used for O_BINARY, etc          */
  18. #include <string.h>                      /* Used for _strchr(), etc         */
  19. #include "jmodem.h"                      /* Used for JMODEM primatives      */
  20. #if !defined (TURBOC)
  21.     #include <sys\types.h>               /* Used to make <sys\stat.h> work  */
  22. #endif
  23. #include <sys\stat.h>                    /* Used for Unix style file I/O    */
  24. #if defined (TURBOC)
  25.     #include <dir.h>                     /* struct for ffblk                */
  26.     #define find_t ffblk
  27.     #define _A_NORMAL 0
  28. #endif
  29.  
  30. #define FCREATE ( O_CREAT | O_BINARY | O_RDWR)   /* Create new file         */
  31. #define FACCESS ( S_IWRITE | S_IREAD )           /* File access mode        */
  32. #define FOPENRD ( O_RDONLY | O_BINARY)           /* Open for read only      */
  33. #define FAIL -1                                  /* Open close create fail  */
  34. const byte old[]=".OLD";                         /* For renaming files      */
  35.  
  36. /****************************************************************************/
  37. word file_io(word command,                       /* Read/write/open, etc.   */
  38.                 register short *handle,          /* File handle             */
  39.                 register byte *buffer,           /* Working buffer pointer  */
  40.                 word len)                        /* Bytes to read/write     */
  41. {
  42.     struct find_t o_file;                        /* To get file size        */
  43.     byte temp[65];                               /* To rename a file        */
  44.     byte *dot;                                   /* Find the "."            */
  45.     switch (command)                             /* GOTO in disguise        */
  46.     {
  47. /****************************************************************************/
  48. /*      Open the file for read. If the open fails, return non-zero.         */
  49. /****************************************************************************/
  50.         case OPEN_READ:
  51.         {
  52.             syst.s_txt = buffer;
  53.             screen (SCR_FIL,&syst);
  54.             if ( (*handle = open(buffer,FOPENRD) ) != FAIL )
  55.             {
  56. #if defined (TURBOC)
  57.                 findfirst(buffer,&o_file,_A_NORMAL);
  58.                 syst.s_byt = o_file.ff_fsize;
  59. #else
  60.                 _dos_findfirst(buffer,_A_NORMAL, &o_file);
  61.                 syst.s_byt = o_file.size;        /* Show file size          */
  62. #endif
  63.                 screen (SCR_FOK,&syst);          /* Show success            */
  64.                 return JM_NRM;
  65.             }
  66.         screen (SCR_FNF);                        /* Show failure            */
  67.         return JM_FNF;
  68.         }
  69. /****************************************************************************/
  70. /*         Create a new file. If the file already exists, rename it.        */
  71. /****************************************************************************/
  72.        case CREATE:
  73.         {
  74.             syst.s_txt = buffer;                 /* Buffer has filename     */
  75.             screen (SCR_FIL,&syst);              /* Write "opening file"    */
  76.             if ( (*handle = open  (              /* See if it exists        */
  77.                             buffer ,             /* Filename                */
  78.                             FOPENRD )            /* Open for read only      */
  79.                             ) != FAIL )
  80.             {                                    /* If file already exists  */
  81.                 close (*handle);                 /* Close the open file     */
  82.                 strcpy (temp,buffer);            /* Copy the file name      */
  83.                 dot = strchr (temp, '.');        /* Find "." in string      */
  84.                 if (dot)                         /* If there was a dot      */
  85.                     strcpy (dot, old);           /* Substitute ".OLD"       */
  86.                 else
  87.                     strcat (temp, old);          /* Append "filename.OLD"   */
  88.                 syst.s_txt = temp;
  89.                 if ( rename(buffer,temp) )       /* Try to rename the file  */
  90.                 {
  91.                     screen (SCR_FCR,&syst);      /* Tell user can't rename  */
  92.                     return JM_REN;               /* Quit                    */
  93.                 }
  94.                 screen (SCR_FRN,&syst);          /* Tell user we renamed    */
  95.             }
  96.             if  ( (*handle = open (              /* Open (create) new file  */
  97.                              buffer ,            /* File name in buffer     */
  98.                              FCREATE ,           /* Create new file         */
  99.                              FACCESS )           /* Define access mode      */
  100.                              ) != FAIL)          /* If successfull          */
  101.             {
  102.                 screen (SCR_FOK,(SYS *) 0x0000); /* Show success            */
  103.                 return JM_NRM;
  104.             }
  105.             screen (SCR_FCR);                    /* Show failure            */
  106.             return JM_CRE;
  107.         }
  108. /****************************************************************************/
  109. /*        Write 'len' bytes to the file. Return the actual bytes written    */
  110. /****************************************************************************/
  111.         case WRITE:
  112.             return (write (*handle, buffer, len));
  113. /****************************************************************************/
  114. /*        Read 'len' bytes from the file. Return the actual bytes read.     */
  115. /****************************************************************************/
  116.         case READ:
  117.             return (read (*handle, buffer, len));
  118. /****************************************************************************/
  119. /*        Close the file. Return any error-code (ignored).                  */
  120. /****************************************************************************/
  121.         case CLOSE:
  122.             return (close (*handle));
  123. /****************************************************************************/
  124. /*        Delete the file named in 'buffer'. Return any error code.         */
  125. /****************************************************************************/
  126.         case DELETE:
  127.             return (remove (buffer));
  128.     }
  129.     return JM_ABT;                                          /* Bad function */
  130. }
  131. /****************************************************************************/
  132. /************************ E N D  O F  M O D U L E  **************************/
  133.